package com.iheima.reggie_take.config;

import com.iheima.reggie_take.common.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * ClassName: WebMvcConfig
 * Package: com.iheima.reggie_take.config
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-01-16
 * Time: 14:19
 */
@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    /**
     * 设置静态资源的映射
     * @param registry
     */

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // registry ruai吉斯吹 注册表
        //使用registry 设置要映射的哪些路径,访问这个路径的哪些资源文件
        log.info("开始静态资源的映射");
        //add Resource Handler 添加资源处理程序 /backend/** : 表示backend路径下所有资源可以被映射
        //add Resource Locations 添加资源的位置 将资源映射到 "classpath:/front/" 路径下  classpath 对应的就是resource的目录
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");

        //没有以上配置 可以直接将资源文件放置static目录下 若配置了 则删除static目录 直接放到resource下
    }


    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建消息转换器
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //设置对象转换器
        converter.setObjectMapper(new JacksonObjectMapper());
        //将上面的消息转换器对象追加到mvc框架的转换器集合中

        converters.add(0,converter);
    }
}
